package com.example.helloandroid;


import android.app.Activity;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import java.util.LinkedList;


public class HelloAndroid extends Activity {
	String message;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = new TextView(this);
        setContentView(tv);
        
        //tv.setText("test", BufferType.EDITABLE);
        /*
        // This allows us to later extend the text buffer.
        tv.setText("test", TextView.BufferType.EDITABLE);
        Editable text = (Editable)tv.getText();
        text.append(" whatever.");
        setContentView(tv);*/
        
        /* Setting up handler for ProgressBar 
        Handler myHandle = new Handler(){
             @Override
               public void handleMessage(Message msg) {

                // get the value from the Message 
            	 
                tv.setText(msg.arg1);

               }

        }; 
        
        Message progressMsg = new Message();
        progressMsg.arg1 = 2;
        myHandle.sendMessage(progressMsg);*/
        
        
        final Runnable mUpdateUITimerTask = new Runnable() {
            public void run() {
                // this if/else is temporary, only using it to clean 
            	// up the UI for now
            	// ** PLAN TO REVISE ** revise so that we remove the beginning
            	// part of the message, which will tell us whether we are appending
            	// or setting the texts
                if(message.startsWith("Player"))
                {
                	tv.setText(message);
                }
                else
                {
                	tv.append(message);
                }
            }
        };
        
        final Handler mHandler = new Handler();	// Associated main window bc of its location
        
        // Here is the heavy-duty thread
        Thread t = new Thread() {
            public void run() {                
                //Message msg1 = new Message();
                //mHandler.sendMessage(msg1);
                
                Deck deck = new Deck();
                deck.generateCards();
                LinkedList<Human> players = new LinkedList<Human>();
                
                int numPlayers = 2; // Get Number of players ( Default 2 )
                
                // NOTE generate players: Need to change the following code
                // Start generate players
                players.add(new Human());
                
                for(int i = 0; i < numPlayers; i++)
                {
                	players.add(new Human());
                }
                // End generate players
                
                deck.shuffle();	// shuffle the deck
                
                // Possibly change it from 52 to deck.size
                int handSize = (int)Math.floor((double)52 / (double)players.size());
                
                // distribute cards among players
                for(int i = 0; i < players.size(); i++)
                {
                	players.get(i).assignCards(deck.getCards( i*handSize, (i+1)*handSize-1 ));
                }
                // start game
                

                message = "Game Beginning\n";
                mHandler.post(mUpdateUITimerTask);
                
                boolean gameDone = false;
                
                int currentCard = 0;
                int currentPlayer = 0;
                LinkedList<Card> cardStack = new LinkedList<Card>();
                int winner = -1;
                
                while(!gameDone)
                {   

                    message = "Player " + currentPlayer + "'s turn\n";
                    mHandler.post(mUpdateUITimerTask);
                    
                	//tv.setText("Player" + currentPlayer + "turn");	// Need to change to show cpu/player
                    players.get(currentPlayer).showCards();			// Display players card        		
                	
                    // Get Player Input
                	// ** IMPLEMENT THIS **
                    
                    players.get(currentPlayer).size();
                    LinkedList<Card> rCards = players.get(currentPlayer).getMove(currentCard);
                    cardStack.addAll(rCards);

                    message = "Throwing away " + rCards.size() + " cards\n";
                    mHandler.post(mUpdateUITimerTask);
                    
                    // Check if player wants to call BS
                    // ** IMPLEMENT THIS ** 
                    
                	if(players.get(currentPlayer).isFinished())
                	{
                		winner = currentPlayer;
                		gameDone = true;
                	}        
                    	
                    currentPlayer++;
                    currentPlayer = currentPlayer % players.size();
                	currentCard++;
                	currentCard = currentCard % 13;	// increment current card 
                }
                
                // Show who winner is
                message = "Winner is " + winner + "\n";
                mHandler.post(mUpdateUITimerTask);
                
                finish();		// exit Activity
            }
        };
        
        t.start();	// Start the thread
    }
}